package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.Goods;
import com.neusoft.ecommerce.entity.PriceType;
import com.neusoft.ecommerce.service.GoodsService;
import com.neusoft.ecommerce.service.PriceTypeService;
/**
 * 
 * @author Administrator
 *
 */
@RestController
public class PriceTypeController {

	private PriceTypeService priceTypeService;
	private GoodsService goodsService;
	
	public PriceTypeController(PriceTypeService priceTypeService, GoodsService goodsService) {
		this.priceTypeService = priceTypeService;
		this.goodsService = goodsService;
	}
	
	@GetMapping("/priceType_page")
    public ModelAndView priceTypePage() {
        return new ModelAndView("priceType/priceType-list");
    }
	
    @RequestMapping("/priceType/list")
    @ResponseBody
    public LayuiPager<PriceType> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<PriceType> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = priceTypeService.findTotal(pager);
        List<PriceType> list = priceTypeService.findList(pager);
        for (PriceType priceType : list) {
            System.out.println(priceType);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
	
	@GetMapping("/addpriceType")
    public ModelAndView addpriceType() {
        return new ModelAndView("priceType/priceType-add");
    }
	
	@RequestMapping("/priceType/goodsList")
    public List<Goods> findGoodsList() {
	    return goodsService.findAll();
    }
	
	@PostMapping("/priceType/add")
    public Map<String, Object> add(PriceType priceType) {
        System.out.println(priceType);
        Map<String, Object> map = new HashMap<>(16);
        if (priceTypeService.add(priceType)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
	@GetMapping("/priceType/edit_page")
    public ModelAndView editPage(PriceType priceType, Model model) {
        System.out.println(priceType);
        LayuiPager<PriceType> pager = new LayuiPager<>(1,1);
        pager.setCondition(priceType);
        PriceType message = priceTypeService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("priceType/priceType-edit");
        model.addAttribute("priceType", message);
        return view;
    }
	

	@PostMapping("/priceType/edit")
    public Map<String, Object> edit(PriceType priceType) {
        System.out.println(priceType);
        Map<String, Object> map = new HashMap<>(16);
        if (priceTypeService.edit(priceType)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/priceType/delete")
    public Map<String, Object> delete(PriceType priceType) {
        System.out.println(priceType);
        Map<String, Object> map = new HashMap<>(16);
        if (priceTypeService.delete(priceType)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
}
